package com.ycy.lucene.search;

import com.ycy.lucene.index.IndexReadDemo;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.index.PointValues;
import org.apache.lucene.search.PointRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermRangeQuery;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RangeQuery {

  private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

  public void search01() {
    TermRangeQuery termRangeQuery = TermRangeQuery.newStringRange("name",
            "a", "b", false, false);
    IndexReadDemo.search(termRangeQuery, 10, null);
  }

  public void search02() {
//    Query query = NumericDocValuesField.newRangeQuery("opendate", 1386172800000L, 1530720000000L);
    Query query = NumericDocValuesField.newSlowRangeQuery("opendate", 0L, 1530720000000L);
//      String content  = "opendate:[-9223372036854775808 TO 9223372036854775807]";
//      QueryParser queryParser = new QueryParser("opendate",null);
//      Query parse = queryParser.parse(content);
    IndexReadDemo.search(query, 10, null);
  }

  /**
   * 精确查找
   */
  public void search03() {
    try {
      Date start = simpleDateFormat.parse("20180105");
      //精确查询
      Query query = NumericDocValuesField.newSlowExactQuery("opendate", start.getTime());
      IndexReadDemo.search(query, 10, null);
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }

  public void search04(){
    int[] min = {35, 50}; // 最小点坐标
    int[] max = {45, 70}; // 最大点坐标
    Query query = IntPoint.newRangeQuery("address", min, max);

  }


}
